package hu.crs.game.statespace;

import hu.crs.game.entity.Entity;
import hu.crs.game.entity.creature.hero.Hero;
import hu.crs.game.field.Field;
import hu.crs.game.state.StateHolder;

import java.util.ArrayList;
import java.util.List;

public class MapElement implements StateHolder {
	private static final long serialVersionUID = 7751905682223821134L;

	Field field;

	List<Entity> entities = new ArrayList<Entity>();

	public MapElement(Field f) {
		field = f;
	}

	public List<Entity> getEntities() {
		return entities;
	}

	public Entity getMainEntity() {
		int i=1;
		int maxIndex=0;
		while (i<entities.size()) {
			if (entities.get(i).getPriority()>entities.get(maxIndex).getPriority()) {
				maxIndex=i;
			}
			i++;
		}
		return entities.get(maxIndex);
	}

	public char getMapElementChar() {
		char mapElementChar=field.getCharRepresentation();
		if (entities.size()>0) {
			mapElementChar=getMainEntity().getCharRepresentation();
		}
		return mapElementChar;
	}
	
	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}
	
	public void removeHero() {
		int i=0;
		while (!(entities.get(i) instanceof Hero)) {
			i++;
		}
		entities.remove(i);
	}
}
